//
//  MqForumModuleCommentViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/18.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumModuleCommentViewController.h"
#import "MqForumDetailTableViewCell.h"
#import "MqForumDetailModel.h"
@interface MqForumModuleCommentViewController ()
<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger startPage;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MqForumModuleCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    _startPage = 1;
    _dataArray = [NSMutableArray array];
    
    //  白色背景色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //  导航栏button:设置, 分享, 收藏
    UIBarButtonItem *setBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_menu@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(settings:)];
    
    UIBarButtonItem *shareBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_share@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(share:)];
    [shareBarItem setBackButtonBackgroundImage:[UIImage imageNamed:@"btn_share_highlighted@2x"] forState:(UIControlStateHighlighted) barMetrics:UIBarMetricsDefault];
    
    UIBarButtonItem *collectionBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_collection_new@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(collection:)];
    [collectionBarItem setBackButtonBackgroundImage:[UIImage imageNamed:@"btn_collection_selected@2x"] forState:(UIControlStateSelected) barMetrics:UIBarMetricsDefault];
    
    //  把button添加进导航栏的右边
    self.navigationItem.rightBarButtonItems = @[setBarItem, shareBarItem, collectionBarItem];
    
    
    [self dataProcess];
    [self addTableView];

}

- (void)settings:(UIBarButtonItem *)barButton{
    
    
}

- (void)share:(UIBarButtonItem *)barButton{
    
    
}

- (void)collection:(UIBarButtonItem *)barButton{
    
    
}



- (void)addTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    
    //  设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    //  tableView表头设置
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    view.backgroundColor = [UIColor redColor];
    //    NSLog(@"%@", _forumModel);
    
    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth - 40, 40)];
    subjectLabel.text = _mainModel.subject;
    subjectLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:subjectLabel];
    
    _tableView.tableHeaderView = view;
    
    _tableView.rowHeight = 180;
    
    //  注册cell
    [_tableView registerClass:[MqForumDetailTableViewCell class] forCellReuseIdentifier:@"MqForumDetailTableViewCell"];
    
}


//  数据处理
- (void)dataProcess{
    
    NSString *path = [NSString stringWithFormat:@"http://bbs.biketo.com/api/mobile/index.php?module=viewthread&page=%ld&ppp=10&tid=%@&version=4",(long)_startPage, _mainModel.tid];
    
    [PKRequestManager requestWithUrlString:path parDic:nil Method:GET finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        if (dic != nil) {
            
            if (_startPage == 1) {
                
                [_dataArray removeAllObjects];
                
            }
            
        }
        
        NSArray *listArray = dic[@"Variables"][@"postlist"];
        
        for (NSDictionary *oneDic in listArray) {
            
            MqForumDetailModel *model = [[MqForumDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            
            [_dataArray addObject:model];
            
        }
        
        
//        NSLog(@"");
        
        //  刷新tableView
        [_tableView reloadData];

        
    } error:^(NSError *error) {
        
        
    } path:nil];
    
    
}

#pragma mark --- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqForumDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MqForumDetailTableViewCell" forIndexPath:indexPath];
    
    MqForumDetailModel *model = _dataArray[indexPath.row];
    
    cell.detailModel = model;
    NSString *xmlString = model.message;
    xmlString = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",kScreenWidth - 40 ,xmlString];
    for (NSString *img in model.imagelist) {
        
        NSDictionary *dic = model.picDic[img];
        
        NSString *attachment = dic[@"attachment"];
        
        NSString *attchicon = dic[@"attachicon"];
        
        //  拼接字符串
        NSString *path = [NSString stringWithFormat:@"http://bbs.biketo.com/%@%@",dic[@"url"], attachment];
        
        NSString *str = [attchicon stringByReplacingOccurrencesOfString:@"static/image/filetype/image.gif" withString:path];
        
        xmlString = [xmlString stringByAppendingString:str];
        
    }
    [cell.messageWebV loadHTMLString:xmlString baseURL:nil];
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
